package com.uetoken.cn.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.uetoken.cn.R;
import com.uetoken.cn.activity.WebActivity;
import com.uetoken.cn.adapter.model.AssetsMainModel;
import com.uetoken.cn.common.Constant;
import com.uetoken.cn.utils.BlockchainUtils;
import com.uetoken.cn.utils.GlideApp;
import com.uetoken.cn.utils.GlideUtils;
import com.uetoken.cn.utils.Save2DecimalUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 可展开收起的Adapter。他跟普通的{@linkGroupedListAdapter}基本是一样的。
 * 它只是利用了{@link GroupedRecyclerViewAdapter}的
 * 删除一组里的所有子项{@link GroupedRecyclerViewAdapter#notifyChildrenRemoved(int)}} 和
 * 插入一组里的所有子项{@link GroupedRecyclerViewAdapter#notifyChildrenInserted(int)}
 * 两个方法达到列表的展开和收起的效果。
 * 这种列表类似于{@link ExpandableListView}的效果。
 * 这里我把列表的组尾去掉是为了效果上更像ExpandableListView。
 */
public class AssetsMainRecyclerAdapter extends GroupedRecyclerViewAdapter {

    private List<AssetsMainModel> mGroups;

    public AssetsMainRecyclerAdapter(Context context, List<AssetsMainModel> groups) {
        super(context);
        this.mGroups = groups;
    }

    public void setData(List<AssetsMainModel> groups) {
        this.mGroups = groups;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //如果当前组收起，就直接返回0，否则才返回子项数。这是实现列表展开和收起的关键。
        if (!isExpand(groupPosition)) {
            return 0;
        }
        List<AssetsMainModel.AssetsDetailModel> children = mGroups.get(groupPosition).getmAssetsDetailModelList();
        return children == null ? 0 : children.size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return false;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.main_assets_recyclerview_item;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return 0;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.sub_assets_recyclerview_item;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        AssetsMainModel entity = mGroups.get(groupPosition);
        GlideApp.with(mContext).load(entity.getImageResource()).apply(GlideUtils.getOtherOption()).into((ImageView) holder.get(R.id.iconIv));
        holder.setText(R.id.mainTextView, (String) entity.getTitleText());
        holder.setText(R.id.detailTextView, (String) entity.getDetailText());
        ImageView ivState = holder.get(R.id.AccessoryIv);
        if (entity.isExpand()) {
            ivState.setImageResource(R.mipmap.icon_arrow_up);
            holder.setVisible(R.id.line2, true);
        } else {
            ivState.setImageResource(R.mipmap.icon_arrow_down);
            if (mGroups.size() - 1 != groupPosition) {
                holder.setVisible(R.id.line2, false);
            }
        }
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        final AssetsMainModel.AssetsDetailModel entity = mGroups.get(groupPosition).getmAssetsDetailModelList().get(childPosition);
        final String purseName = (String) entity.getSubitemText();
        int[] purseInfo = BlockchainUtils.getPurseIcon(purseName);
        int purseIcon = purseInfo[0];
        int newScale = purseInfo[1];
        String subDetailText;
        String balance;
        final boolean isShowMarket;
        if (entity.getPursetypeid() == 3) {
            subDetailText = mContext.getString(R.string.str_in_trading) + " " + entity.getTransactionamount();
            balance = entity.getBalanceformat();
            isShowMarket = true;
            int drawable;
            int textColor;
            if (ObjectUtils.equals("1", entity.getFlag())) {//涨跌(1=涨，0=跌)
//                holder.setImageResource(R.id.marketIv, purseName);//ContextCompat.getDrawable(mContext, purseIcon)
                drawable = R.mipmap.icon_market_up;
                textColor = R.color.verdancyColor;
            } else {
                drawable = R.mipmap.icon_market_down;
                textColor = R.color.redColor2;
            }
            String price = entity.getPrice();
            if (ObjectUtils.isEmpty(price) || ObjectUtils.equals("null", price)) {
                price = "0.00";
            }
            holder.setText(R.id.exchangeRateTv, "￥" + price);
            holder.setTextColor(R.id.exchangeRateTv, ContextCompat.getColor(mContext, textColor));
            holder.setImageResource(R.id.marketIv, drawable);
        } else {
            isShowMarket = false;
            subDetailText = mContext.getString(R.string.str_freeze) + " " + entity.getSubitemAmount();
            balance = entity.getAmount();
        }
        final String monetaryUnit = entity.getDataBean().getPurseunitname();
        holder.setVisible(R.id.marketLayout, isShowMarket);
        holder.setText(R.id.subTextView, purseName);
        holder.setText(R.id.subNumDetailTextView, balance);
        holder.setText(R.id.subDetailTextView, subDetailText);
        holder.get(R.id.purseNameLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowMarket) {
//                    https://widget.lianxueqiu.com/market/pages/markettokendetails/index.html?
//                    currency=BTC&apikey=yjs82v29ck2&ccy=USD&theme=dark&language=en
                    String apikey = "43188f51bf894e54";

                    String url = "https://widget.lianxueqiu.com/market/pages/markettokendetails/index.html?currency="
                            + purseName + "&apikey=" + apikey + "&ccy=" + monetaryUnit + "&theme=dark&language=en";

                    Intent intent = new Intent(mContext, WebActivity.class);
                    intent.putExtra(Constant.TO_WEBVIEW_URL, url);
                    mContext.startActivity(intent);
                }
            }
        });

    }

    /**
     * 判断当前组是否展开
     *
     * @param groupPosition
     * @return
     */
    public boolean isExpand(int groupPosition) {
        AssetsMainModel entity = mGroups.get(groupPosition);
        return entity.isExpand();
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     */
    public void expandGroup(int groupPosition) {
        expandGroup(groupPosition, false);
    }

    /**
     * 展开一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void expandGroup(int groupPosition, boolean animate) {
        AssetsMainModel entity = mGroups.get(groupPosition);
        entity.setExpand(true);
        if (animate) {
            notifyChildrenInserted(groupPosition);
        } else {
            notifyDataChanged();
        }
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     */
    public void collapseGroup(int groupPosition) {
        collapseGroup(groupPosition, false);
    }

    /**
     * 收起一个组
     *
     * @param groupPosition
     * @param animate
     */
    public void collapseGroup(int groupPosition, boolean animate) {
        AssetsMainModel entity = mGroups.get(groupPosition);
        entity.setExpand(false);
        if (animate) {
            notifyChildrenRemoved(groupPosition);
        } else {
            notifyDataChanged();
        }
    }
}
